﻿//   StoredProcedureResult.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA
namespace CKSAS.AppStorage.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    /// <summary>
    /// Represents the result of a stored procedure execution and provides 
    /// automatic disposal and clean up of a result object
    /// </summary>
    internal sealed class StoredProcedureResult : IDisposable
    {
        /// <summary>
        /// Gets or sets the execution type of the stored procedure
        /// </summary>
        /// <value>
        /// The type of the execution.
        /// </value>
        public StoredProcedureExecutionType ExecutionType { get; set; }

        /// <summary>
        /// Gets or sets the scalar return value
        /// </summary>
        /// <value>
        /// The scalar value.
        /// </value>
        public object ScalarValue { get; set; }

        /// <summary>
        /// Gets or sets the number of records affected in the last stored procedure execution
        /// </summary>
        /// <value>
        /// The records affected.
        /// </value>
        public int RecordsAffected { get; set; }

        /// <summary>
        /// Gets or sets the data reader that points to the result of a stored procedure.
        /// </summary>
        /// <value>
        /// The data reader.
        /// </value>
        public SqlDataReader DataReader { get; set; }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        void IDisposable.Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                // release the reader is allocated
                switch (this.ExecutionType)
                {
                    case StoredProcedureExecutionType.ExecuteReader:
                        {
                            // check if a reader is active
                            if (this.DataReader != null)
                            {
                                if (!this.DataReader.IsClosed)
                                {
                                    this.DataReader.Close();
                                }

                                // dispose
                                this.DataReader.Dispose();
                            }

                            break;
                        }
                }
            }
        }
    }
}